//
//  ViewController.m
//  EverydayNews
//
//  Created by 李沛池 on 2017/7/18.
//  Copyright © 2017年 Godlike. All rights reserved.
//

#import "HomeViewController.h"
#import <BotBrainPods/BotBrainManager.h>
#import "ENLoginViewController.h"
#import "UserModel.h"
#import "LPCCommentApp.h"
#import "LPCShareView.h"
#import "LPCNotificationManager.h"
#import "ENDynamicPlistManager.h"
#import "AppDelegate.h"

@interface HomeViewController ()<
BotBrainFeedDetailDelegate>

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self loadAD];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}

- (void)initData
{
    //注册通知
    [self addNotifications];
    
    if (![BotBrainManager defaultManager].isLoggedIn) {
        //如果本地存在用户信息 则默认登录
        if ([UserModel isLogin]) {
            [self logInCommentSystem];
        }
    }
}

- (void)addNotifications
{
    //启动页加载完成
    [[LPCNotificationManager sharedLPCNotificationManager] addObserver:LPCNotificationType_doneLaunchAD obj:nil sel:@selector(donelaunchAD) controll:self];
}

- (void)initUI
{
    [[BotBrainManager defaultManager] addBotBrainFeedOnView:self.view inViewController:self];
    [[BotBrainManager defaultManager] addFeedDetailDelegate:self];
}

- (void)updateUI
{
    self.tabBarController.navigationItem.title = @"财经观察";
}

- (void)logInCommentSystem
{
    UserModel *model = [UserModel sharedUserModel];
    BotBrainUserInfoModel *userInfo = [BotBrainUserInfoModel new];
    userInfo.userID = model.userID;
    userInfo.userIcon = model.userIcon;
    userInfo.userNickName = model.userNickName;
    [[BotBrainManager defaultManager] loginWithUserInfo:userInfo success:^(BotBrainUserInfoModel *userInfo) {
        NSLog(@"UserInfo：%@", userInfo);
    } failure:^(NSError *error) {
        NSLog(@"Error：%@", error);
    }];
}

//加载广告
- (void)loadAD
{
    PresentToVC(@"ENADViewController",NO);
}

//广告页加载完成
- (void)donelaunchAD
{
    [LPCCommentApp configCommentLogicWithTitle:@"财经观察" message:@"看官，给我们个鼓励吧！" commentAppBlock:^{
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:Address_APPStore_Comment]];
    } feedbackBlock:^{
        PushToNibVC(@"ENFeedbackViewController");
    }];
    
    GCD_DELAY(0.3, ^{
        //如果有推送 跳转详情
        NSString *pushId = ((AppDelegate *)[UIApplication sharedApplication].delegate).pushId;
        if (pushId.length > 0) {
            [[BotBrainManager defaultManager] showBotDetailControllerWithItemID:pushId];
        }
    });
}

//分享到平台
- (void)shareToPlatform:(SharePlatformType)type shareModel:(BotBrainFeedShareModel *)shareModel
{
    UMSocialPlatformType platformType = UMSocialPlatformType_WechatSession;
    switch (type) {
        case SharePlatformType_WechatSession:
        {
            platformType = UMSocialPlatformType_WechatSession;
        }
            break;
        case SharePlatformType_WechatTimeLine:
        {
            platformType = UMSocialPlatformType_WechatTimeLine;
        }
            break;
        case SharePlatformType_QQ:
        {
            platformType = UMSocialPlatformType_QQ;
        }
            break;
        case SharePlatformType_Sina:
        {
            platformType = UMSocialPlatformType_Sina;
        }
            break;
        default:
            break;
    }
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    //创建网页内容对象
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:shareModel.feedItemTitle descr:shareModel.feedItemSummary thumImage:@"https://mobile.umeng.com/images/pic/home/social/img-1.png"];
    //设置网页地址
    shareObject.webpageUrl = shareModel.feedItemURL;
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            [LPCMBProgressHUD showErrorWithText:@"分享失败"];
            UMSocialLogInfo(@"************Share fail with error %@*********",error);
        }else{
            if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = data;
                
                [[BotBrainManager defaultManager] shareBotBrainFeedSuccess];
                [ENDynamicPlistManager saveDynamic:[NSString stringWithFormat:@"您分享了“%@”%@%@",shareModel.feedItemTitle,SEPARATE_string,[LPCTools getDateWithFormatter:@"MM-dd"]]];
                
                //分享结果消息
                UMSocialLogInfo(@"response message is %@",resp.message);
                //第三方原始返回的数据
                UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
                
            }else{
                UMSocialLogInfo(@"response data is %@",data);
            }
        }
    }];
}


#pragma mark - View
//分享view
- (void)showShareView:(BotBrainFeedShareModel *)shareModel
{
    [LPCShareView showInWindow:^(SharePlatformType type) {
        [self shareToPlatform:type shareModel:shareModel];
    }];
}


#pragma mark - Delegate

// 分享 - 点击分享按钮时
- (void)botBrainDidShareWithShareModel:(BotBrainFeedShareModel *)shareModel
{
    if ([BotBrainManager defaultManager].isLoggedIn) {
        [self showShareView:shareModel];
    }else {
        [self pushToLoginVC];
    }
}

// 点赞 - 点赞时
- (void)botBrainDidLikeWithShareModel:(BotBrainFeedShareModel *)shareModel
{
    if ([BotBrainManager defaultManager].isLoggedIn) {
        [ENDynamicPlistManager saveDynamic:[NSString stringWithFormat:@"您赞了“%@”%@%@",shareModel.feedItemTitle,SEPARATE_string,[LPCTools getDateWithFormatter:@"MM-dd"]]];
    }else {
        [self pushToLoginVC];
    }
}

//发布评论 - 将要回复评论时（可实现此方法来提示用户登录）
- (void)botBrainWillCommentWithShareModel:(BotBrainFeedShareModel *)shareModel
{
    if ([BotBrainManager defaultManager].isLoggedIn) {
        
    }else {
        UIViewController *currentVC = [BotBrainManager currentShowViewController];
        if ([currentVC isKindOfClass:[NSClassFromString(@"BotAllCommetController") class]]) {
            [currentVC dismissViewControllerAnimated:NO completion:^{
                [self pushToLoginVC];
            }];
        }else {
            [self pushToLoginVC];
        }
    }
}

- (void)botBrainDidCommentWithShareModel:(BotBrainFeedShareModel *)shareModel
{
    if ([BotBrainManager defaultManager].isLoggedIn) {
        [ENDynamicPlistManager saveDynamic:[NSString stringWithFormat:@"您评论了“%@”%@%@",shareModel.feedItemTitle,SEPARATE_string,[LPCTools getDateWithFormatter:@"MM-dd"]]];
    }
}

//跳转到登陆页
- (void)pushToLoginVC
{
    ENLoginViewController *enlVC = StoryboardXIIBVC(@"ENLoginViewController");
    [self.navigationController pushViewController:enlVC animated:YES];
}

- (BOOL)feedShouldForbidRoll {
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
